<template>
  <div>
    <t-modal
      :title="operate == 1 ? '新建文件夹' : '编辑文件夹'"
      width="900px"
      :visible="visible"
      @cancel="closeModal()"
      @ok="handleOk()"
      wrapClassName="component-search-container"
      forceRender
      :confirmLoading="submitLoading"
    >
      <a-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item ref="nodeName" label="文件夹名称" prop="nodeName">
          <a-input v-model="form.nodeName" placeholder="请输入文件夹名称" />
        </a-form-model-item>
      </a-form-model>
    </t-modal>
  </div>
</template>
<script>
import { axios } from "@/utils/request";
export default {
  name: "addModal",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    operate: {
      type: Number,
      default: 1,
    },
    pid: {
      type: [Number, String],
      default: 0,
    },
    nodeName: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      submitLoading: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      form: {
        //文件名称
        nodeName: "",
      },
      rules: {
        nodeName: [
          {
            required: true,
            message: "请输入文件夹名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          if (this.operate == 2) {
            //新增
            this.form.nodeName = this.nodeName;
          } else {
            //编辑
            this.form.nodeName = "";
          }
        }
      },
    },
  },
  mounted() {},
  methods: {
    //确定
    handleOk() {
      this.submitLoading = true;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.operate == 1) {
            //新增
            this.handleAdd();
          } else {
            //编辑
            this.handleEdit();
          }
        } else {
          return false;
        }
      });
    },

    //新增
    handleAdd() {
      let { pid } = this;
      let { nodeName } = this.form;
      let { proCode } = this.$parent.condition;
      axios
        .post(
          `/probusiness/bimCadClassify/save`,
          {
            proCode,
            nodeName,
            pid,
          },
          {
            headers: {
              "Content-Type": "application/json",
            },
          }
        )
        .then((res) => {
          if (res.statusCode == 200) {
            this.$message.success("新建成功！");
            this.$parent.getDrawingTreeData();
            this.closeModal();
          } else {
            this.$message.error("新建失败，请稍后重试~");
          }
        })
        .catch((_) => {
          reject();
        })
        .finally((_) => {
          this.submitLoading = false;
        });
    },

    //编辑
    handleEdit() {
      let { pid } = this;
      let { nodeName } = this.form;
      let { proCode } = this.$parent.condition;
      axios
        .post(
          `/probusiness/bimCadClassify/updateData`,
          {
            proCode: proCode,
            nodeName,
            uuid: pid,
          },
          {
            headers: {
              "Content-Type": "application/json",
            },
          }
        )
        .then((res) => {
          if (res.statusCode == 200) {
            this.$message.success("编辑成功！");
            this.$parent.getDrawingTreeData();
            this.closeModal();
          } else {
            this.$message.error("编辑失败，请稍后重试~");
          }
        })
        .catch((_) => {
          reject();
        })
        .finally((_) => {
          this.submitLoading = false;
        });
    },

    //关闭
    closeModal() {
      setTimeout((_) => {
        this.form.nodeName = "";
        this.$refs?.ruleForm?.resetFields();
      }, 500);
      this.$emit("update:visible", false);
    },
  },
};
</script>
<style lang="less" scoped></style>
